package com.gack.business.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.gack.business.vo.UserInfoEnterpriseVO;
import com.gack.business.vo.UserInfoOrdersVO;

/**
 * 
* @ClassName: UserManageInfoDao 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author (ZhangXinYu)  
* @date 2018年6月20日 上午9:18:15 
* @version V1.0
 */
@Repository
public class UserManageInfoDao {
	
	@Autowired
	private EntityManager entityManager;
	
	/**
	 * 获取用户详情
	 */
	public UserInfonobind getUserInfo(String userid){
		UserInfonobind userInfonobind = null;
		String jpql = "select"
						+ " u.id,u.username,IFNULL(u.nickname,u.username) as nickname,u.last_login_time,u.check_time,u.createtime,"
						+ " u.is_check as ischeck,u.is_pledge as ispledge,IFNULL(u.create_office_num ,0) as create_office_num,"
						+ " IFNULL(u.create_meeting_num,0) as create_meeting_num,IFNULL(u.join_video_num,0) as join_video_num,"
						+ " IFNULL(u.total_join_meeting_time,0) as total_join_meeting_time,IFNULL(u.total_office_time,0) as total_office_time,"
						+ " IFNULL(u.expense_num,0) as expense_num,IFNULL(u.expense_amount,0) as expense_amount,"
						+ " u.portrait,u.`status` as `status`"
					+ " from user u"
					+ " where u.id = :userid";
		Query query = entityManager.createNativeQuery(jpql,UserInfonobind.class);
		query.setParameter("userid", userid);
		userInfonobind = (UserInfonobind)query.getSingleResult();
		return userInfonobind;
	}
	
	/**
	 * 获取用户对应公司
	 */
	public List<UserInfoEnterpriseVO> getUserEnterprise(String userid){
		List<UserInfoEnterpriseVO> enterpriseVOs = null;
		String jpql = "select "
						+ " e.id,e.name"
						+ " from user_enterprise_department_position uedp "
					+ " LEFT JOIN enterprise e on uedp.enterprise_id = e.id"
					+ " WHERE uedp.user_id = :userid";
		Query query = entityManager.createNativeQuery(jpql,UserInfoEnterpriseVO.class);
		query.setParameter("userid", userid);
		enterpriseVOs = (List<UserInfoEnterpriseVO>)query.getResultList();
		return enterpriseVOs;
	}
	
	/**
	 * 获取用户对应订单
	 */
	public List<UserInfoOrdersVO> getUserOrders(String userid){
		List<UserInfoOrdersVO> ordersVOs = null;
		String jpql = "select "
						+ " o.id,o.order_id,o.type,s.store_name as address,o.created_time,"
						+ " IFNULL(o.pay_amount,0) as pay_amount"
					+ " from orders o"
					+ " LEFT JOIN stores s on o.store_id = s.id"
					+ " WHERE (o.status = '8' or o.status = '9' or o.status = '10')"
					+ " and o.user_id = :userid";
		Query query = entityManager.createNativeQuery(jpql,UserInfoOrdersVO.class);
		query.setParameter("userid", userid);
		ordersVOs = (List<UserInfoOrdersVO>)query.getResultList();
		return ordersVOs;
	}
}
